package com.fyt.algorithm.Leecode.day3;

import java.util.*;

public class GroupAnagrams {
    /**
     *
     * 个人想法   使用hashmap嵌套hashmap试试
     * @param strs
     * @return
     */
/*    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<Map,Integer> map1=new HashMap<>();
        List<List<String>> res=new ArrayList<>();
        int index=0;

        for (int i = 0; i <strs.length ; i++) {
          //  if(map.containsKey())
           String str= strs[i];
            Map<Character,Integer> map3=new HashMap<>();
            for (int j = 0; j <str.length() ; j++) {
               char c= str.charAt(j);
                map3.put(c,map3.getOrDefault(c,0)+1);
            }

            if(map1.containsKey(map3)){

              List list  =res.get(map1.get(map3));

              list.add(str);
            }
            else {

                //添加到结果集,开辟一个新的list
                List<String> list=new ArrayList();
                list.add(str);
                res.add(index,list);
                map1.put(map3,index);
                index++;


            }

        }


        return res;
    }*/


    /**
     * chatGPT的排序法
     * @param strs
     * @return
     */
    public static List<List<String>> groupAnagrams(String[] strs){

        //键为顺序后的唯一值
        Map<String,List<String>> map=new HashMap<>();

        for(String str:strs){
            char[] array  =str.toCharArray();
            Arrays.sort(array);
            String key=Arrays.toString(array);
            map.computeIfAbsent(key,k->new ArrayList<String>()).add(str);
        }


        return  new ArrayList<>(map.values());
    }



    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(groupAnagrams(strs));



       /* Map<Integer,Integer>map1=new HashMap<>();
        Map<Integer,Integer>map2=new HashMap<>();
        Map<Map,Integer> map3=new HashMap<>();

        map1.put(1,3);
        map2.put(1,3);
        map3.put(map1,1);
        System.out.println(map3.containsKey(map2));*/



    }

}
